#include "stdafx.h"
#include "td_tower.h"
#include "td_bomb.h"
#include "td_resource.h"
#include "td_enemy.h"
td_tower::td_tower()
{
}
td_tower::~td_tower()
{
	/*vector<td_bomb *>::iterator iter;
	for (iter = vec_bomb.begin(); iter != vec_bomb.end(); iter++)
	{
		delete (*iter);
	}
	vec_bomb.clear();*/
}
void td_tower::readyxy(int x, int y)
{
	xy_tower.x = x;
	xy_tower.y = y;
}
void td_tower::get_tower(IplImage *one)
{
	tower = one;
}
void td_tower::draw_tower(IplImage *background)
{
	for (int i = 0;i < 70;i++)
	{
		for (int j = 0;j < 70;j++)
		{
			if (CV_IMAGE_ELEM(tower, uchar, i, j * 3 + 0) < 250 ||
				CV_IMAGE_ELEM(tower, uchar, i, j * 3 + 1) < 250 ||
				CV_IMAGE_ELEM(tower, uchar, i, j * 3 + 2) < 250)
			{
				CV_IMAGE_ELEM(background, uchar, i + xy_tower.x, (j + xy_tower.y) * 3 + 0) =
					CV_IMAGE_ELEM(tower, uchar, i, j * 3 + 0);
				CV_IMAGE_ELEM(background, uchar, i + xy_tower.x, (j + xy_tower.y) * 3 + 1) =
					CV_IMAGE_ELEM(tower, uchar, i, j * 3 + 1);
				CV_IMAGE_ELEM(background, uchar, i + xy_tower.x, (j + xy_tower.y) * 3 + 2) =
					CV_IMAGE_ELEM(tower, uchar, i, j * 3 + 2);
			}
		}
	}

	

}
void td_tower::creatbomb(td_enemy *enemy1,IplImage *bombpic)
{
	//cout << vec_bomb.size()<<endl;
	/*cout << vec_bomb.size() << endl;
	for (int i = 0;i < vec_bomb.size();i++)
	{
		if (vec_bomb[i]->enemy == enemy1)return;
	}*/
	if (vec_bomb.size() > 0)return;
	td_bomb *bomb1=new td_bomb;
	bomb1->enemy = enemy1;
	bomb1->xy_bomb.x = xy_tower.x+35;
	bomb1->xy_bomb.y = xy_tower.y+35;
	bomb1->velocity_bomb = shoot_velocity;
	bomb1->intput_bombpic(bombpic);
	vec_bomb.push_back(bomb1);
}
int td_tower::judge_enemy(td_enemy *enemy,IplImage *bombpic)
{
	int a = (enemy->get_xy().x - xy_tower.x)*(enemy->get_xy().x - xy_tower.x) +
		(enemy->get_xy().y - xy_tower.y)*(enemy->get_xy().y - xy_tower.y);
	if (a <= shoot_scope)
	{
		creatbomb(enemy,bombpic);    
		a = 1;
	} 
	return a;
}
void td_tower::drow_bonb(IplImage *background)
{
	vector<td_bomb *>::iterator iter;
	for (iter = vec_bomb.begin(); iter != vec_bomb.end(); iter++)
	{
		(*iter)->draw_bomb(background);
	}
}
void td_tower::shoot_enemy()
{
	vector<td_bomb *>::iterator iter;
	for (iter = vec_bomb.begin(); iter != vec_bomb.end(); iter++)
	{
		int a = (*iter)->shoot_enemy();
	}

}
void td_tower::judge_bomb()
{
	vector<td_bomb *>::iterator iter;
	iter = vec_bomb.begin();
	while(iter != vec_bomb.end())
	{
		if (((*iter)->xy_bomb.x >= (*iter)->enemy->get_xy().x+15 &&
			(*iter)->xy_bomb.y >= (*iter)->enemy->get_xy().y+15 &&
			(*iter)->xy_bomb.x <= (*iter)->enemy->get_xy().x + 45 &&
			(*iter)->xy_bomb.y <= (*iter)->enemy->get_xy().y + 45))
			{
				(*iter)->enemy->speed_enemy = 2;
				(*iter)->enemy->hp_enemy -= hurt_tower;
				if ((*iter)->enemy->hp_enemy < 0)(*iter)->enemy->hp_enemy = 0;
			}
		iter++;
	}
}
void td_tower::delete_bomb()
{
	vector<td_bomb *>::iterator iter;
	iter = vec_bomb.begin();
	while (iter != vec_bomb.end())
	{
		if (((*iter)->xy_bomb.x >= (*iter)->enemy->get_xy().x + 15 &&
			(*iter)->xy_bomb.y >= (*iter)->enemy->get_xy().y + 15 &&
			(*iter)->xy_bomb.x <= (*iter)->enemy->get_xy().x + 45 &&
			(*iter)->xy_bomb.y <= (*iter)->enemy->get_xy().y + 45) 
			||(*iter)->enemy->hp_enemy==0)
		{
			delete(*iter);
			iter = vec_bomb.erase(iter);
			continue;
		}
		iter++;
	}
}
